var IE = document.all ? true : false;
var OPERA = navigator.userAgent.toLowerCase().indexOf("opera") != -1;
var smintval = null;

$(document).ready(function()
{
	showMessage('Ładowanie strony Szkoły Jazdy Kopaczewski...', 'notice', false);
	
	$(document).pngFix();
	
	$('.messageBox').corner('bottom');
	$('div.fotothumb').corner('7px');
	
	$("a.fancy").fancybox(
	{
		'padding': 0,
		'frameWidth': 480,
		'frameHeight': 360,
		'zoomSpeedIn': 300, 
		'zoomSpeedOut':	300, 
		'hideOnContentClick': false,
		'overlayShow': false
	});
	
	$("a.fancyImg").fancybox(
	{
		'zoomSpeedIn': 300, 
		'zoomSpeedOut':	300, 
		'hideOnContentClick': true,
		'overlayShow': false
	});	
	
	$("a.fancyImgInvisible").fancybox(
	{
		'frameWidth': 920,
		'frameHeight': 780,		
		'zoomSpeedIn': 500, 
		'zoomSpeedOut':	500, 
		'hideOnContentClick': true,
		'overlayShow': true,
		'centerOnScroll': false
	});
	
	$('.linkCursor').bind('mouseenter mouseleave', function()
	{
		//w ff nie dziala kursor hand
		if (OPERA || IE)
		{
			$(this).toggleClass('cursorHand1');
		}
		else
		{
			$(this).toggleClass('cursorHand2');
		}
	});
	
	//linki do podstron
	$('.meetingBox').bind('click', function()
	{
		window.location.href = '/Instruktorzy';
	});
	
	$('.schoolBox').bind('click', function()
	{
		window.location.href = '/Szkola';
	});
	
	doSwimmingMessageAnimation();
});

function showContent()
{
	hideMessage();
	$('.loaderBox').hide();
	
	setTimeout(function() {
		 $("a.fancyImgInvisible").trigger("click");
	}, 1500);
}

function clearFormFields()
{
	$('#name').val('');
	$('#email').val('');
	$('#title').val('');
	$('#message').val('');
}

function checkFormFields(nameInput, emailInput, messageInput)
{
	if(nameInput == null || $.trim(nameInput).length == 0)
	{
		showMessage('Brak imienia i nazwiskia.', 'notice', true);
		return 1;
	}
	
	if(emailInput == null || $.trim(emailInput).length == 0)
	{
		showMessage('Nie podano adresu email.', 'notice', true);
		return 2;			
	}
	
	if(emailInput.indexOf('@') < 0 || emailInput.indexOf('.') < 0)
	{
		showMessage('Błędny format adresu email.', 'notice', true);
		return 2;
	}
	
	if(messageInput == null || $.trim(messageInput).length == 0)
	{
		showMessage('Pusta wiadomość.', 'notice', true);
		return 3;
	}

	return 0;	
}
 
function sendMail(url)
{
	var name = $("#name").val();
	var email = $("#email").val();
	var message = $("#message").val();

	var formFieldCorrect = checkFormFields(name, email, message);
	
	if(formFieldCorrect == 0)
	{
		showMessage('Wysyłanie wiadomości...', 'notice', false);
		
		$.post(url, {name_input: name, email_input: email, message_input: message}, function(data)
		{
			if(data == 'true') 
			{
				clearFormFields();
				
				hideMessage(200, function()
				{
					showMessage('Wiadomość została wysłana.', 'correct', true);
				});
				
			}
			else 
			{
				hideMessage(200, function()
				{
					showMessage('Błąd wysyłania wiadomości.', 'error', true);
				});
				
			}
	   	});
	}
}

function showMap()
{
	var w = window.open ('http://www.zumi.pl/1852625,Kopaczewski_Szkola_Nauki_Jazdy,Bydgoszcz,firma.html?src=2', 'Zumi','status=0,toolbar=0,menubar=0,height=670,width=1024');	
}

function showMessage(message, type, isDisapear)
{
	$.scrollTo(0, {duration:1000});
	$('.messageCanvas').text(message).removeClass('correct error notice').addClass(type);
	$('.messageBox').slideDown("normal");
	
	if(isDisapear)
	{
		setTimeout(function ()
		{
			hideMessage();	
		}, 5000);
	}
}

function hideMessage(speed, callback)
{
	$('.messageBox').slideUp("normal", callback);
}

function doSwimmingMessageAnimation() {
	var screenWidth = ($(window).width() < 1420 ? 1420 : $(window).width())  ;
	var messageBlockCount = $('div.swimmingmessage').find('div.floatleft').length;

	if (messageBlockCount > 1) {
		var messageBlockWidth = screenWidth / (messageBlockCount-1);
		messageBlockWidth += 40;
		var timestamp = 0;
		
		$('div.swimmingmessage').find('div.floatleft').each(function(index, value) {
			var position = messageBlockWidth*(-1+index);
			
			$(this).css('left', position).css('width', messageBlockWidth);
		});
		
		smintval = setInterval(function() {
			$('div.swimmingmessage').find('div.floatleft').each(function(index, value) {
				var leftString = $(this).css('left');
				var left = parseFloat(leftString.replace('px', ''));
				
				if (left > screenWidth) {
					left = messageBlockWidth * -1;
				}
				else {
					left += 1;
				}
				
				$(this).css('left', left);
			});
			
			timestamp += 30;
		}, 30);
		
		$('div.swimmingMessageBoxClose a').click(function() {
			$(this).hide();
			$('div.swimmingmessage').hide();
			clearInterval(smintval);
		});
	}
}